﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient ;
using System.Reflection;
using System.Resources;
using System.ComponentModel;
using System.IO;
using System.Configuration;
using Dtl;

namespace Dal
{
    public class UserDao
    {
        public static int SetUser(User cUser)
        {
            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return -1;
            int insert = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "SetUser";
            if (cUser.Id == 0)
            {
                cmd.Parameters.AddWithValue("@idUser", null);
            }
            else {
                cmd.Parameters.AddWithValue("@idUser", cUser.Id);
            }
            
            cmd.Parameters.AddWithValue("@dsName", cUser.Name);
            cmd.Parameters.AddWithValue("@dsUserName", cUser.UserName);
            cmd.Parameters.AddWithValue("@dsPassword", cUser.Password);
            cmd.Parameters.AddWithValue("@dsEmail", cUser.Email);
            cmd.Parameters.AddWithValue("@dsSkype", cUser.Skype);
            cmd.Parameters.AddWithValue("@dsMSN", cUser.MSN);
            cmd.Parameters.AddWithValue("@dsPhone1", cUser.Phone1);
            cmd.Parameters.AddWithValue("@dsPhone2", cUser.Phone2);
            cmd.Parameters.AddWithValue("@idRole", cUser.Role);
            cmd.Parameters.AddWithValue("@idStatus", cUser.Status);
            try
            {
                cmd.Connection.Open();
                insert = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (SqlException exc)
            {
                Credentials.writeLogFile("SetUser: " + exc.Message);
                return -1;
            }
            return insert;
        }

        public static int SetUserPassword(string Username, String Password)
        {
            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return -1;
            int insert = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "SetUserPassword";
            cmd.Parameters.AddWithValue("@UserName", Username);
            cmd.Parameters.AddWithValue("@Password", Password);
            try
            {
                cmd.Connection.Open();
                insert = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch (SqlException exc)
            {
                Credentials.writeLogFile("SetPassword: " + exc.Message);
                return -1;
            }
            return insert;
        }

        public static int ValidateUser(string Username, string Password)
        {
            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return -1;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Username", Username));
            cmd.Parameters.Add(new SqlParameter("@Password", Password));
            cmd.CommandText = "ValidateUser";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("ValidateUser: " + ex.Message);
                return -1;
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            cmd.Connection.Close();
            if (dt.Rows.Count > 0)
            {
                return int.Parse(dt.Rows[0]["idRole"].ToString());
            }
            else {
                return 0;
            }
        }

        public static DataSet getRols()
        {
            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "GetRoles";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("rolesDT");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getRols: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("rolesDS");
            ds.Tables.Add(dt);
            cmd.Connection.Close();
            return ds;
        }

        public static DataSet getUserbyUserName(string UserName)
        {
            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Username", UserName));
            cmd.CommandText = "GetUserbyUserName";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("userDT");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getUserbyUserName: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("userDS");
            ds.Tables.Add(dt);
            cmd.Connection.Close();
            return ds;
        }

        public static DataSet getUsers() {
            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "GetUsers";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("usersDT");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getUsers: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("usersDS");
            ds.Tables.Add(dt);
            cmd.Connection.Close();
            return ds;
        }

        //public static User GetUserByDistributor(int idDistributor)
        //{
        //    Database database = DatabaseFactory.CreateDatabase("DmsTools");

        //    using (DbCommand dbCommand = database.GetStoredProcCommand("GetUserByDistributor"))
        //    {
        //        database.AddInParameter(dbCommand, "@IdDistributor", DbType.String, idDistributor);
        //        IDataReader reader = database.ExecuteReader(dbCommand);
        //        User user = new User();
        //        while (reader.Read())
        //        {

        //            user.Id = Convert.ToInt32(reader["IdUser"]);
        //            user.Name = Convert.ToString(reader["dsName"]);
        //            user.UserName = Convert.ToString(reader["dsUserName"]);
        //            //user.Email = Convert.ToString(reader["dsEmail"]);
        //            //user.Phone1 = Convert.ToString(reader["dsPhone1"]);
        //            //user.Phone2 = Convert.ToString(reader["dsPhone2"]);
        //            //user.MSN = Convert.ToString(reader["dsMSN"]);
        //            //user.Skype = Convert.ToString(reader["dsSkype"]);
        //            user.Role = Convert.ToInt32(reader["idRole"]);
        //            //user.Skype = Convert.ToString(reader["idStatus"]);
        //        }

        //        reader.Close();

        //        return user;
        //    }
        //}

        //public DataSet GetUserbyRole(int idRole)
        //{
        //    List<User> users = new List<User>();
        //    Database database = DatabaseFactory.CreateDatabase("DmsTools");
        //    using (DbCommand dbCommand = database.GetStoredProcCommand("GetUserbyRole"))
        //    {
        //        database.AddInParameter(dbCommand, "@IdRole", DbType.String, idRole);
        //        IDataReader reader = database.ExecuteReader(dbCommand);
        //        while (reader.Read())
        //        {
        //            User user = new User();
        //            user.Id = Convert.ToInt32(reader["IdUser"]);
        //            user.Name = Convert.ToString(reader["dsName"]);
        //            user.UserName = Convert.ToString(reader["dsUserName"]);
        //            user.Email = Convert.ToString(reader["dsEmail"]);
        //            user.Phone1 = Convert.ToString(reader["dsPhone1"]);
        //            user.Phone2 = Convert.ToString(reader["dsPhone2"]);
        //            user.MSN = Convert.ToString(reader["dsMSN"]);
        //            user.Skype = Convert.ToString(reader["dsSkype"]);
        //            user.Role = Convert.ToInt32(reader["idRole"]);
        //            user.Skype = Convert.ToString(reader["idStatus"]);
        //            users.Add(user);
        //        }
        //        reader.Close();
        //        return users;
        //    }
    }
}

